package sg.edu.nus.iss.cats.action;

import javax.servlet.http.*;
import org.apache.struts.action.*;
import org.apache.commons.beanutils.BeanUtils;

import sg.edu.nus.iss.cats.form.*;
import sg.edu.nus.iss.cats.model.*;
import sg.edu.nus.iss.cats.controller.*;
import sg.edu.nus.iss.cats.dataaccess.*;
import sg.edu.nus.iss.cats.exception.*;

public class UserUpdateAction extends Action {

    public ActionForward execute(ActionMapping mapping,
                                 ActionForm form,
                                 HttpServletRequest request,
                                 HttpServletResponse response)
                         throws DAOException, UserUpdateException {
        System.out.println("UserUpdateAction");

        User user = new User();
        UserForm formBean = (UserForm) form;

        try {
            BeanUtils.copyProperties(user, formBean);
        } catch (Exception e) {
            throw new UserUpdateException("Could not populate the User: " + e);
        }
        
        System.out.println("Going to update user");
        UserManager userMgr = new UserManager();
        userMgr.updateUser(user);

        System.out.println("UserUpdateAction successful");
        return mapping.findForward("userEdit");
    }

}